import { defineStore } from 'pinia';

/**
 * 工作流状态管理存储
 */
export const useWorkflowStore = defineStore('workflow', {
  state: () => ({
    // 患者基础信息
    patientInfo: {
      id: '',
      name: '',
      age: '',
      gender: '',
      department: ''
    },
    
    // 步骤1: CT上传与分割
    ctUpload: {
      images: [] as any[],
      segmentationResults: [] as any[]
    },
    
    // 步骤2: 病历报告上传
    medicalRecord: null as any,
    
    // CTClip预测结果
    ctClipPredictions: {
      success: false,
      predictions: [] as { label: string; probability: number }[],
      topPrediction: null as { label: string; probability: number } | null
    },
    
    // 步骤3: 诊断报告
    diagnosisReport: null as any
  }),
  
  getters: {
    // 是否完成了CT上传和分割
    hasCompletedCtUpload: (state) => state.ctUpload.segmentationResults.length > 0,
    
    // 是否完成了病历上传
    hasCompletedMedicalRecord: (state) => state.medicalRecord !== null,
    
    // 是否有CTClip预测结果
    hasCtClipPredictions: (state) => state.ctClipPredictions.success && state.ctClipPredictions.topPrediction !== null,
    
    // 获取CTClip的顶部预测结果
    topCtClipPrediction: (state) => state.ctClipPredictions.topPrediction?.label || '',
    
    // 获取所有预测结果，按概率降序排序
    sortedCtClipPredictions: (state) => [...state.ctClipPredictions.predictions].sort((a, b) => b.probability - a.probability)
  },
  
  actions: {
    /**
     * 保存CT上传与分割结果
     */
    saveCTUploadResults(data: any) {
      this.ctUpload.images = data.ctImages || [];
      this.ctUpload.segmentationResults = data.segmentationResults || [];
    },
    
    /**
     * 保存病历报告
     */
    saveMedicalRecord(data: any) {
      this.medicalRecord = data;
    },
    
    /**
     * 保存CTClip预测结果
     */
    saveCtClipPredictions(data: any) {
      this.ctClipPredictions = {
        success: data.success || false,
        predictions: data.predictions || [],
        topPrediction: data.top_prediction || null
      };
    },
    
    /**
     * 保存诊断报告
     */
    saveDiagnosisReport(data: any) {
      this.diagnosisReport = data;
    },
    
    /**
     * 更新患者信息
     */
    updatePatientInfo(data: any) {
      this.patientInfo = {
        ...this.patientInfo,
        ...data
      };
    },
    
    /**
     * 重置工作流数据
     */
    resetWorkflow() {
      this.ctUpload.images = [];
      this.ctUpload.segmentationResults = [];
      this.medicalRecord = null;
      this.ctClipPredictions = {
        success: false,
        predictions: [],
        topPrediction: null
      };
      this.diagnosisReport = null;
    }
  }
}); 